<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>用户列表</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/resources/css/base.css" rel="stylesheet"
	type="text/css" />
<link href="/resources/js/miniui/themes/default/miniui.css"
	rel="stylesheet" type="text/css" />
<script src="/resources/js/jquery-1.6.2.min.js"
	type="text/javascript"></script>
<script src="/resources/js/miniui/miniui.js"
	type="text/javascript"></script>
<script src="/resources/js/ckeditor/ckeditor.js"
	type="text/javascript"></script>
<style type="text/css">
.errorText {
	color: red;
}
#editWindow {top: 50px !important;left:250px!important}
.mini-checkboxlist-item{    float: left;}
</style>
</head>
<body>
	<h1>试题列表</h1>
	 <form id="file_form" action="/question/import" enctype="multipart/form-data" method="post">
	 
		<div style="padding-top: 5px; padding-bottom: 5px; width: 95%;">
			<a class="mini-button"
				img="/resources/js/miniui/themes/icons/add.gif"
				onclick="load4add()">添加</a> 
			<a class="mini-button"
				img="/resources/js/miniui/themes/icons/edit.gif"
				onclick="editRow()">修改</a> 
			<a class="mini-button"
				img="/resources/js/miniui/themes/icons/remove.gif"
				onclick="delRow()">删除</a> 	
		   
	        <input type="file" style="margin-left:60px" name="file" id="file" /> 
	        <input type="submit" style="margin-left:-40px" value="批量导入excel" id='upFile-btn'>
		    		
		</div>
	 </form>	
	<div id="mini-datagrid" class="mini-datagrid"
		style="width: 95%; height: 400px;"
		url="/question/selectByPage" pageSize="20" idField="id"
		allowResize="true">

		<div property="columns">
			<div type="indexcolumn">序号</div>
			<div type="checkcolumn">选择</div>
			<div field="course_id">课程id</div>
			<div field="title">题目</div>
			<div field="answer_a">答案A</div>
			<div field="answer_b">答案B</div>
			<div field="answer_c">答案C</div>
			<div field="answer_d">答案D</div>
			<div field="correct">正确答案</div>
			<div field="type">试题类型</div>
		</div>
	</div>

	<div id="editWindow" class="mini-window" title="添加试题"
		style="width: 400px;" showModal="true" allowResize="true"
		allowDrag="true">
		<div id="editform" class="form" enctype="multipart/form-data">
			<input class="mini-hidden" name="id" value="0" />
			<table style="width: 100%; text-align: center;">
				<tr>
					<td style="width: 30%; text-align: right;">课程id</td>
					<td style="width: 60%; text-align: left;">
						<input name="course_id" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="course_id_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">题目类型：</td>
					<td style="width: 60%; text-align: left;">
						<input name="type" class="mini-radiobuttonlist" data="[{id: 1, text: '选择题'}, {id: 2, text: '判断题'}]"/>						
					</td>
					<td style="width: 10%;" id="type_error" class="errorText"></td>
				</tr>				
				<tr>
					<td style="width: 30%; text-align: right">题目：</td>
					<td style="width: 60%; text-align: left;">
						<input name="title" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="title_error" class="errorText"></td>
				</tr>
				
				<tr>
					<td style="width: 30%; text-align: right;">答案A：</td>
					<td style="width: 60%; text-align: left;">
						<input name="answer_a" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="answer_a_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right;">答案B：</td>
					<td style="width: 60%; text-align: left;">
						<input name="answer_b" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="answer_b_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right;">答案C：</td>
					<td style="width: 60%; text-align: left;">
						<input name="answer_c" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="answer_c_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right;">答案D：</td>
					<td style="width: 60%; text-align: left;">
						<input name="answer_d" class="mini-textbox" />
					</td>
					<td style="width: 10%;" id="answer_d_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right;">正确答案：</td>
					<td style="width: 60%; text-align: left;">
						<input name="correct" class="mini-radiobuttonlist" data="[{id: 1, text: 'A'}, {id: 2, text: 'B'}, {id: 3, text: 'C'}, {id: 4, text: 'D'}]"/>						
					</td>
					<td style="width: 10%;" id="correct_error" class="errorText"></td>
				</tr>												
				
				<tr>
					<td
						style="text-align: center; padding-top: 5px; padding-right: 20px;"
						colspan="3"><a class="mini-button"
						img="/resources/js/miniui/themes/icons/save.gif"
						onclick="saveBaseRow()">保存</a> <a class="mini-button"
						img="/resources/js/miniui/themes/icons/cancel.gif"
						onclick="cancelRow()">取消</a></td>
				</tr>
			</table>
		</div>
	</div>

</body>

<script type="text/javascript" src="/resources/js/ajaxfileupload.js"></script>
<script type="text/javascript">
	mini.parse();
	var datagrid = mini.get("mini-datagrid");
	var editWindow = mini.get("editWindow");
	
	var form = new mini.Form("editform");
	var actionType = "insertQuestion";
	datagrid.load();
	
	function load4add() {
		actionType = "insertQuestion";
		editWindow.show();
		form.clear();
	}

	function saveBaseRow() {
		var json = form.getData();
		if (!validation(json))
			return;
		
		saveRow(json);
	}
	
	function cancelRow() {
		datagrid.reload();
		editWindow.hide();
	}
	
	function saveRow(json) {
		console.log(json);
		$.ajax({
			url : "/question/" + actionType,
			data : json,
			type : "post",
			dataType : "json",
			success : function(data) {
				if(data.code == 0){
					editWindow.hide();
					datagrid.reload();
				}else{
					mini.alert(data.data);
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});

	}	
	
	function validation(data) {
		if (data.id == 'undefined' || data.id == '') {
			data.id = 0;
		}
		
/* 		if (data.realname == '') {
			$('#realname_error').html("真实姓名不能为空");
			return false;
		} else {
			$('#realname_error').html("");
		}

		if (data.idNo == '') {
			$('#idNo_error').html("身份证号码不能为空");
			return false;
		} else {
			$('#idNo_error').html("");
		}

		if (data.diplomaName == '') {
			$('#diplomaName_error').html("证书名称不能为空");
			return false;
		} else {
			$('#diplomaName_error').html("");
		}
		
		if (data.diplomaImg == '') {
			$('#diplomaImg_error').html("证书图片不能为空");
			return false;
		} else {
			$('#diplomaImg_error').html("");
		}	 */	

		return true;
	}	
	
	function editRow() {

		var node = datagrid.getSelected();
		if (node) {
			actionType = "updateQuestion";
			editWindow.show();
			form.clear();
			
			$.ajax({
				url : "/question/getById",
				data : {id:node.id},
				type : "post",
				dataType : "json",
				success : function(data) {
					form.setData(node);	
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert(jqXHR.responseText);
				}
			});			
		} else {
			mini.alert("请选择一条!");
		}
	}	
	
	function delRow() {

		var node = datagrid.getSelected();
		if (node) {
			mini.confirm("确定删除！", "提示", function(retCode) {
				console.log(retCode);
				if (retCode == 'ok') {
					
					$.ajax({
						url : "/question/deleteQuestion",
						data : {
							id : node.id
						},
						type : "post",
						dataType : "json",
						success : function() {
							editWindow.hide();
							datagrid.reload();
						},
						error : function(jqXHR, textStatus, errorThrown) {
							alert(jqXHR.responseText);
						}
					});

				}
			})
		} else {
			mini.alert("请选择一条!");
		}
	}	
	
	$('#upFile-btn').click(function(){
		 $.ajaxFileUpload({  
	            url: "/question/import", //用于文件上传的服务器端请求地址  
	            secureuri: false, //一般设置为false  
	            fileElementId: 'file', //文件上传空间的id属性  <input type="file" id="file" name="file" />  
	            dataType: 'json', //返回值类型 一般设置为json  
	            type:"post",  
	            success: function (data){  
	            	if (data.code == 0) {
	            		alert("导入成功！！！！");
	            		window.location.reload();
	            	}
	            },  
	            error: function(XMLHttpRequest, textStatus, errorThrown) {
	            	 alert(XMLHttpRequest.status);
	            	 alert(XMLHttpRequest.readyState);
	            	 alert(textStatus);
	            }
	        });  
	    return false; 
	})
</script>

</html>
